package shiqi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shiqi.service.user.UserService;
import shiqi.vo.UserVo;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @program: ascend
 * @description: 用户控制类
 * @author: shiqi
 * @create: 2022-03-17 16:43
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    //@Autowired 只能按类型注入，有多个同接口的实现类时会注入失败，可以用@Qualifier指定bean名称解决，或用@Resource指定名称或类型
    @Autowired
    @Qualifier("userService")
//    @Resource
    private UserService userService;

    @Resource(name = "mapUserService")
    private UserService mapUserService;

    //也可以用map接受所有实现，string为bean的名字
    @Autowired
    private Map<String, UserService> userServiceMap;

    /**
     * @author shiqi
     * @createTime 2022/3/17 16:46
     * @desc 登录接口 测试路径：/user/login?userName=shiqi&password=123456
     * @param userVo
     * @return java.lang.String
     */
    @GetMapping("/login")
    public String login(UserVo userVo) {
        boolean result = false;
        try {
            //result = userService.login(userVo);
            //result = mapUserService.login(userVo);
            result = userServiceMap.get("mapUserService").login(userVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ? "登录成功" : "登录失败";
    }

    @GetMapping("searchById")
    public Object searchById(Long id) {
        return userService.searchById(id);
    }
}
